﻿package {
	import quiz.QuizFormat;

	import fl.controls.Label;

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * @author Moritz Bode
	 */
	public class Quiz extends MovieClip {

//		var urlRequest : URLRequest = new URLRequest("quiz/Questions.xml");
//		var urlLoader : URLLoader = new URLLoader();
		var xml : XML;
		var xmlList : XMLList;
		var i : int = 0;
		var qf : QuizFormat;
		var qscore : int;
		private var seriousGame : SeriousGame;

		public function Quiz() {
			urlLoaded();
		}

		function urlLoaded() : void {
			xml = 
<Questions>  
  <Q1>
   <mainQ>In Australia, how is the date of Mothers' Day calculated?
   </mainQ>
   <RBC>4</RBC>
   <A1>It's always the second Saturday in May.</A1>
   <A2>It's always the second Sunday in May.</A2>
   <A3>It's always the second Sunday in June.</A3>
   <A4>It's always the second Saturday in June.</A4>
   <ANS>It's always the second Sunday in May.</ANS>
  </Q1>

  <Q2>
   <mainQ>What is the world's largest desert?</mainQ>
   <RBC>4</RBC>
   <A1>The Simpson</A1>
   <A2>The Kalahari</A2>
   <A3>The Sahara</A3>
   <A4>The Death Valley</A4>
   <ANS>The Sahara</ANS>
  </Q2>

  <Q3>
   <mainQ>What is the last letter of the Greek alphabet?</mainQ>
   <RBC>3</RBC>
   <A1>Gaama</A1>
   <A2>Alpha</A2>
   <A3>Omega</A3>   
   <ANS>Omega</ANS>
  </Q3>

  <Q4>
   <mainQ>Name a state of U.S.A. beginning with `B'.
   </mainQ>
   <RBC>5</RBC>
   <A1>There is none!</A1>
   <A2>Bulgaria</A2>
   <A3>Bhutan</A3> 
   <A4>Barbados</A4>
   <A5>BeliZe</A5>  
   <ANS>There is none!</ANS>
  </Q4>

   <Q5>
   <mainQ>Which is the only continent occupied by one nation?</mainQ>
   <RBC>3</RBC>
   <A1>Asia</A1>
   <A2>Australia</A2>
   <A3>Iropa</A3>   
   <ANS>Australia</ANS>
  </Q5>

   <Q6>
   <mainQ>Name the only boxer to knock out Mohammed Ali
   </mainQ>
   <RBC>3</RBC>
   <A1>Gary Lockett</A1>
   <A2>Shane Mosley</A2>
   <A3>Larry Holmes</A3>   
   <ANS>Larry Holmes</ANS>
  </Q6>

   <Q7>
   <mainQ>How many metres make a kilometre?</mainQ>
   <RBC>6</RBC>
   <A1>100</A1>
   <A2>10</A2>
   <A3>0.1</A3> 
   <A4>1000</A4>
   <A5>0.01</A5>
   <A6>0.001</A6>  
   <ANS>1000</ANS>
  </Q7>

   <Q8>
   <mainQ>What colour are the towers of San Francisco's Golden Gate Bridge?</mainQ>
   <RBC>5</RBC>
   <A1>Green</A1>
   <A2>Joseph Priestly</A2>
   <A3>White</A3> 
   <A4>Red</A4> 
   <A5>Pink</A5> 
   <ANS>Red</ANS>
  </Q8>

   <Q9>
   <mainQ>How many Earth years does it take Pluto to orbit the sun?</mainQ>
   <RBC>6</RBC>
   <A1>248</A1>
   <A2>225</A2>
   <A3>256</A3>
   <A4>272</A4>
   <A5>245</A5>
   <A6>251</A6>   
   <ANS>248</ANS>
  </Q9>

   <Q10>
   <mainQ>How many years of marraige are celebrated by a Golden Wedding Anniversary?   </mainQ>
   <RBC>4</RBC>
   <A1>30</A1>
   <A2>10</A2>
   <A3>25</A3>  
   <A4>50</A4> 
   <ANS>50</ANS>
  </Q10>
</Questions>;
			//xml = XML(event.target.data); //Taking external file as one XML object
			xmlList = xml.children();	//All child tags will come
			init(xmlList,i);
		}

		function init(xList : XMLList,j : int):void {
	
			if(j < xList.length()) {
				var ansarr : Array = new Array();
				for(var k = 0;k < xList[j].RBC;k++) {
					var kk : Object = "A" + (k + 1).toString();
					ansarr[k] = xList[j].child(kk);
			//trace(ansarr);
				}
				qf = new QuizFormat(xList[j].RBC, xList[j].mainQ, ansarr, xList[j].ANS,this);	
				addChild(qf);
			} else {
				displayscore(qscore);
			}
		}

		public function nextques(quscore : int):void {
			qscore = quscore 
			removeChild(qf);
				
			i = i + 1;
			if(i==10){
				i=0;
			}
			init(xmlList, i);		
		}

		function displayscore(quizscore : int) : void {	
			var scoreFormat : TextFormat = new TextFormat();
			scoreFormat.font = "Verdana";
			scoreFormat.bold = true;
			scoreFormat.size = 20;			
			scoreFormat.color = 0xFF0066;
	
			var scoreLabel : Label = new Label();
			scoreLabel.text = "Your Score : " + quizscore.toString();
			scoreLabel.autoSize = TextFieldAutoSize.LEFT;
			scoreLabel.setStyle("textFormat", scoreFormat);
			scoreLabel.move(200, 170);            
			addChild(scoreLabel);
		}
		
		public function nextQuestion():void{
			this.qf.NextQuestion();
		}
		
		public function setTextForCountdown(text:String){
			this.qf.setTextForCountdown(text);
		}
		
		public function isCorrect():Boolean{
			return this.qf.isCorrect();
		}
		
		/*public function checkAnsers():void{
			this.qf.CheckAnswer();
		}*/
		
		public function setSeriuosGame(seriousGame:SeriousGame):void{
			this.seriousGame = seriousGame;
		}
		
		public function getSeriousGame():SeriousGame{
			return this.seriousGame;
		}
		
		
	}
}
